package com.example.backend.service.impl;

import com.example.backend.entity.ProductManufacture;
import com.example.backend.mapper.ProductManufactureMapper;
import com.example.backend.util.AuthUtil;
import com.example.backend.mapper.AccountMapper;
import com.example.backend.service.ManufacturerService;
import com.example.backend.util.WeBASEUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.web3j.utils.Numeric;

import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ManufacturerServiceImpl implements ManufacturerService {

    @Autowired
    private WeBASEUtils weBASEUtils;

    @Autowired
    private AuthUtil authUtil;
    
    @Autowired
    protected AccountMapper accountMapper;
    @Autowired
    private ProductManufactureMapper productManufactureMapper;

    @Override
    public String register(String username, String passWordF, String passwordS, String qualificationContent, int accountType) {
        return authUtil.register(username, passWordF, passwordS, qualificationContent, accountType);
    }
    
    @Override
    public String login(String accountId, String password, int accountType) {
        return authUtil.login(accountId, password, accountType);
    }
    
    @Override
    public String changePassword(String accountId, String oldPassword, String newPassword, int accountType) {
        return authUtil.changePassword(accountId, oldPassword, newPassword, accountType);
    }
    
    @Override
    public String postQualificationContent(String accountId, String qualificationContent, int accountType) {
        return authUtil.postQualificationContent(accountId, qualificationContent, accountType);
    }

    @Override
    public String postGoodsMadeInfo(Map<String, String> credentials) {
        if (credentials.get("manufacturerId") == null|| credentials.get("productName") == null){
            return "参数不足或参数错误";
        }
        LocalDateTime now = LocalDateTime.now();
        String banchId = authUtil.generateGoodsId();
        ProductManufacture productManufacture = new ProductManufacture();
        productManufacture.setBatchId(banchId);
        productManufacture.setProductName(credentials.get("productName"));
        productManufacture.setManufacturerId(credentials.get("manufacturerId"));
        productManufacture.setQualityInspection(Integer.valueOf(credentials.get("qualityInspection")));
        productManufacture.setProductionTime(now);
        productManufactureMapper.insert(productManufacture);
        //转换格式，从localdatatime转换成uint256能兼容的格式
        long epochSecond = now.toEpochSecond(ZoneOffset.ofHours(8));
        String hexTimestamp = Numeric.toHexStringWithPrefix(BigInteger.valueOf(epochSecond));

        try {
            //数据上链（商品批次ID、商品名称、账号、质检信息（0，1）、商品生产时间戳）
            List<Object> param = new ArrayList<>();
            param.add(banchId);
            param.add(credentials.get("productName"));
            param.add(credentials.get("manufacturerId"));
            param.add(credentials.get("qualityInspection"));
            weBASEUtils.funcPost("ProductModule","addProduct",param);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            return "商品信息提交成功";
        }



    }
}